/**
 * Blockly Apps: Blocklycraft
 *
 * Copyright 2012 Google Inc.
 * http://blockly.googlecode.com/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @fileoverview Blocks for ComputerCraft turtles.
 * @author ellen.spertus@gmail.com (Ellen Spertus)
 */
'use strict';

// Extensions to Blockly's language and Lua generator.

var BASE_URL_ = 'http://computercraft.info/wiki/';

Blockly.Blocks['turtle_move'] = {
  // Block for moving forward, back, up, or down.
  init: function() {
    var DIRECTIONS =
        [[BlocklyApps.getMsg('Code_turtleForward'), 'forward'],
         [BlocklyApps.getMsg('Code_turtleBackward'), 'back'],
         [BlocklyApps.getMsg('Code_turtleUp'), 'up'],
         [BlocklyApps.getMsg('Code_turtleDown'), 'down']];
    this.setColour(160);
    this.appendDummyInput()
        .appendTitle(new Blockly.FieldDropdown(DIRECTIONS), 'DIR');
    this.setOutput(true, 'Boolean')
    this.setTooltip(BlocklyApps.getMsg('Code_turtleMoveTooltip'));
    var thisBlock = this;
    this.setHelpUrl(function() {
      var mode = thisBlock.getTitleValue('DIR');
      return BASE_URL_ + 'Turtle.' + mode;
    });
  }
};

Blockly.Lua['turtle_move'] = function(block) {
  // Generate Lua for moving forward, backwards, up, or down.
  var code = 'turtle.' + block.getTitleValue('DIR') + '()';
  return [code, Blockly.Lua.ORDER_HIGH];
};

Blockly.Blocks['turtle_turn'] = {
  // Block for turning left or right.
  init: function() {
    var DIRECTIONS =
        [[BlocklyApps.getMsg('Code_turtleRight'), 'turnRight'],
         [BlocklyApps.getMsg('Code_turtleLeft'), 'turnLeft']];
    this.setColour(160);
    this.appendDummyInput()
        .appendTitle(new Blockly.FieldDropdown(DIRECTIONS), 'DIR');
    this.setPreviousStatement(true);
    this.setNextStatement(true);
    this.setTooltip(BlocklyApps.getMsg('code_turtleTurnTooltip'));
    var thisBlock = this;
    this.setHelpUrl(function() {
      var mode = thisBlock.getTitleValue('DIR');
      return BASE_URL_ + 'Turtle.' + mode;
    });
  }
};

Blockly.Lua['turtle_turn'] = function(block) {
  // Generate Lua for turning left or right.
  return 'turtle.' + block.getTitleValue('DIR') + '()\n';
};

Blockly.Blocks['turtle_dig'] = {
  // Block for digging in front, above, or below the turtle.
  init: function() {
    var DIRECTIONS =
        [[BlocklyApps.getMsg('Code_turtleDigFront'), 'dig'],
         [BlocklyApps.getMsg('Code_turtleDigUp'), 'digUp'],
         [BlocklyApps.getMsg('Code_turtleDigDown'), 'digDown']];
    this.setColour(160);
    this.appendDummyInput()
        .appendTitle(new Blockly.FieldDropdown(DIRECTIONS), 'DIR');
    this.setOutput(true, 'Boolean');
    this.setTooltip(BlocklyApps.getMsg('Code_turtleDigTooltip'));
    var thisBlock = this;
    this.setHelpUrl(function() {
      var mode = thisBlock.getTitleValue('DIR');
      return BASE_URL_ + 'Turtle.' + mode;
    });
  }
};

Blockly.Lua['turtle_dig'] = function(block) {
  // Generate Lua for digging forward, up, or down.
  var code = 'turtle.' + block.getTitleValue('DIR') + '()';
  return [code, Blockly.Lua.ORDER_HIGH];
};

Blockly.Blocks['turtle_detect'] = {
  // Block for detecting in front, above, or below the turtle.
  init: function() {
    var DIRECTIONS =
        [[BlocklyApps.getMsg('Code_turtleDetectFront'), 'detectBlock'],
         [BlocklyApps.getMsg('Code_turtleDetectUp'), 'detectBlockUp'],
         [BlocklyApps.getMsg('Code_turtleDetectDown'), 'detectBlockDown']];
    this.setColour(160);
    this.appendDummyInput()
        .appendTitle(new Blockly.FieldDropdown(DIRECTIONS), 'DIR');
    this.setOutput(true, 'Boolean')
    this.setTooltip(BlocklyApps.getMsg('Code_turtleDetectTooltip'));
    var thisBlock = this;
  }
};

Blockly.Lua['turtle_detect'] = function(block) {
  // Generate Lua for detecting forward, up, or down.
  return ['sensor.' + block.getTitleValue('DIR') + '()', Blockly.Lua.ORDER_HIGH];
};

Blockly.Blocks['turtle_place'] = {
  // Block for placing an item from the selected slot in front of, above,
  // or below the turtle.
  init: function() {
    var DIRECTIONS =
        [[BlocklyApps.getMsg('Code_turtlePlaceFront'), 'place'],
         [BlocklyApps.getMsg('Code_turtlePlaceUp'), 'placeUp'],
         [BlocklyApps.getMsg('Code_turtlePlaceDown'), 'placeDown']];
    this.setColour(160);
    this.appendDummyInput()
        .appendTitle(new Blockly.FieldDropdown(DIRECTIONS), 'DIR');
    this.setOutput(true, 'Boolean')
    this.setTooltip(BlocklyApps.getMsg('Code_turtlePlaceTooltip'));
    var thisBlock = this;
    this.setHelpUrl(function() {
      var mode = thisBlock.getTitleValue('DIR');
      return BASE_URL_ + 'Turtle.' + mode;
    });
  }
};

Blockly.Lua['turtle_place'] = function(block) {
  // Generate Lua for placing forward, up, or down.
  return ['turtle.' + block.getTitleValue('DIR') + '()', Blockly.Lua.ORDER_HIGH];
};

Blockly.Blocks['turtle_place_sign'] = {
  // Block for placing a sign from the selected slot in front of, above,
  // or below the turtle.
  init: function() {
    var DIRECTIONS =
        [[BlocklyApps.getMsg('Code_turtlePlaceSignFront'), 'place'],
         [BlocklyApps.getMsg('Code_turtlePlaceSignUp'), 'placeUp'],
         [BlocklyApps.getMsg('Code_turtlePlaceSignDown'), 'placeDown']];
    this.setColour(160);
    this.appendDummyInput()
        .appendTitle(new Blockly.FieldDropdown(DIRECTIONS), 'DIR');
    this.interpolateMsg(BlocklyApps.getMsg('Code_turtlePlaceSignText'),
                        ['TEXT', 'String', Blockly.ALIGN_RIGHT],
                        Blockly.ALIGN_RIGHT);
    this.setOutput(true, 'Boolean')
    this.setTooltip(BlocklyApps.getMsg('Code_turtlePlaceTooltip'));
    var thisBlock = this;
    this.setHelpUrl(function() {
      var mode = thisBlock.getTitleValue('DIR');
      return BASE_URL_ + 'Turtle.' + mode;
    });
  }
};

Blockly.Lua['turtle_place_sign'] = function(block) {
  // Generate Lua for placing forward, up, or down.
  var text = Blockly.JavaScript.valueToCode(block, 'TEXT',
      Blockly.Lua.ORDER_NONE) || '0';
  return ['turtle.' + block.getTitleValue('DIR') + '(' + text +')',
          Blockly.Lua.ORDER_HIGH];
};

Blockly.Blocks['terminate'] = {
  // Terminate program with error message.
  init: function() {
    this.setColour(160);
    this.interpolateMsg(BlocklyApps.getMsg('Code_terminate'),
                        ['MSG', null, Blockly.ALIGN_RIGHT],
                        Blockly.ALIGN_RIGHT);
    this.setTooltip(BlocklyApps.getMsg('Code_terminateTooltip'));
    // This has a previous, but not a following, statement.
    this.setPreviousStatement(true);
  }
};

Blockly.Lua['terminate'] = function(block) {
  // Generate Lua for terminating program with error message.
  var message = Blockly.JavaScript.valueToCode(block, 'MSG',
      Blockly.Lua.ORDER_NONE) || '"error"';
  return 'error(' + message + ')\n';
};
